﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using WindowsGameInput;



namespace WindowsGame
{
	public class Button: Text
	{

		Texture2D rollOverImg;
		bool rollOver = false;
		bool mousePressed = false;
		bool mouseReleased = false;

		public event Action ButtonClick;



		public Button(Texture2D captionIn, Texture2D rollOverImgIn, Vector2 positionIn)
			:base(captionIn, positionIn)
		{
			rollOverImg = rollOverImgIn;
		}



		public override void Update(GameTime gameTime)
		{
			InputState inputState = InputHandler.GetState();

			rollOver = false;
			mouseReleased = false;

			if(inputState.mouseX >= position.X && inputState.mouseX <= position.X + captionImg.Width)
				if(inputState.mouseY >= position.Y && inputState.mouseY <= position.Y + captionImg.Height)
				{
					if(inputState.mouseLButtonPressed)
						mousePressed = true;
					if(inputState.mouseLButtonReleased)
						mouseReleased = true;
					rollOver = true;
				}

			if(inputState.mouseLButtonReleased && !mouseReleased)
				mousePressed = false;


			if(mousePressed && mouseReleased)
			{
				mousePressed = false;
				if(ButtonClick != null)
					ButtonClick();
			}
		}



		public override void Draw(SpriteBatch spriteBatch, GameTime gameTime, float scale)
		{
			if(rollOver)
				spriteBatch.Draw(rollOverImg, position, null, Color.White, 0.0f, Vector2.Zero, scale, SpriteEffects.None, 0.0f);
			else
				base.Draw(spriteBatch, gameTime, scale);
				
		}
	}
}
